package org.wmq.travelagency02.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.model.Entity.TourGroupTraveller;
import org.wmq.travelagency02.model.Entity.Traveller;
import org.wmq.travelagency02.service.TourGroupService;
import org.wmq.travelagency02.service.TourGroupTravellerService;

import java.util.List;

@RestController
@RequestMapping("tour-group-traveller")
@RequiredArgsConstructor
public class TourGroupTravellerController {
    private final TourGroupTravellerService tourGroupTravellerService;

    @GetMapping("/getTravellerByTourGroupId/{tourGroupId}")
    public Result<List<Traveller>> getTravellersByTourGroupId(@PathVariable Long tourGroupId){
        return Result.ok(tourGroupTravellerService.getTravellersByTourGroupId(tourGroupId));
    }

    @GetMapping("/getTourGroupCurrentSizeByTourGroupId/{tourGroupId}")
    public Result<Long> getTourGroupCurrentSizeByTourGroupId(@PathVariable Long tourGroupId){
        return Result.ok(this.tourGroupTravellerService.getTourGroupCurrentSizeByTourGroupId(tourGroupId));
    }

    @PostMapping("/addTravellerToTourGroup/{travellerId}/{tourGroupId}")
    public Result<Void> addTravellerToTourGroup(@PathVariable Long travellerId, @PathVariable Long tourGroupId){
        boolean success = this.tourGroupTravellerService.addTravellerToTourGroup(travellerId, tourGroupId);
        if (!success) {
            return Result.error("添加游客到旅游团失败");
        }
        return Result.ok();
    }

    @DeleteMapping("/dropTravellerFromTourGroup/{travellerId}/{tourGroupId}")
    public Result<Void> dropTravellerFromTourGroup(@PathVariable Long travellerId, @PathVariable Long tourGroupId){
        boolean success = this.tourGroupTravellerService.removeByTravellerIdAndTourGroupId(travellerId, tourGroupId);
        if (!success) {
            return Result.error("删除游客失败");
        }
        return Result.ok();
    }

}
